package role

import (
	"context"

	"jinrigaoping/api/superadmin/internal/svc"
	"jinrigaoping/api/superadmin/internal/types"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type RolePageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRolePageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RolePageLogic {
	return &RolePageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RolePageLogic) RolePage(req *types.SysRolePageReq) (resp *types.SysRolePageRes, err error) {

	pageResp, err := l.svcCtx.SysRpc.SysRolePage(l.ctx, &sys.SysRolePageReq{
		Page:  req.Page,
		Limit: req.Limit,
	})

	if err != nil {
		return nil, err
	}

	var list = make([]*types.SysRolePageItem, len(pageResp.List))

	for i, v := range pageResp.List {
		list[i] = &types.SysRolePageItem{
			Id:       v.Id,
			RoleName: v.RoleName,
			Remark:   v.Remark,
		}
	}

	return &types.SysRolePageRes{
		List:  list,
		Total: pageResp.Count,
	}, nil
}
